<?php

/**
 * 琴谱Svc
 * User: wangzhi
 * Date: 2017/06/12
 */
class Service_OpernModel {

    private $_opern_Dao;
    private $_category_Dao;
    private $_music_Dao;
    private $_usedopern_Dao;
    private $_userDao;
    private $_graspDao;
    
    const TEACHER_TYPE       = 1;
    const STUDENT_TYPE       = 2;
    const OPERN_NORMAL_STATUS = 1;
    const USEDOPERN_ICON      = 'http://img1.kuwo.cn/star/picServer/66/82/1497324310224.png';

    public function __construct ()
    {
        $this->_opern_Dao     = new Dao_OpernModel();
        $this->_category_Dao  = new Dao_CategoryModel();
        $this->_music_Dao     = new Dao_MusicModel();
        $this->_usedopern_Dao = new Dao_UsedopernModel();
        $this->_userDao     = new Dao_UserModel();
        $this->_graspDao = new Dao_StudentgraspopernModel();
    }

    /*
     * 获取琴谱列表
     */

    public function getOpernList ($uid , $sid)
    {
        Service_UserModel::userVail ( $uid , $sid );
        //获取所有分类
        $categoryList = $this->_category_Dao->getCategoryList ();
        $list         = array ();
        $used         = array ();
        foreach ( $categoryList as $key => $category )
        {
            $categoryList[ $key ][ 'list' ] = $this->_opern_Dao->getOpernByCid ( $category[ 'id' ] , 10 );
        }
        $useddata       = $this->getusedopern ( $uid , $sid );
        $used[ 'name' ] = '最近使用';
        $used[ 'icon' ] = self::USEDOPERN_ICON;
        $used[ 'list' ] = $useddata;
        $categoryList[] = $used;
        return $categoryList;
    }

    /*
     * 通过分类查找下面所有曲谱
     */

    public function getOpernByCid ($uid , $sid , $categoryid)
    {
        Service_UserModel::userVail ( $uid , $sid );
        if ( $categoryid == 0 ) throw new YExceptionTool ( YExceptionTool::PARAMPACK_ERROR );
        $list = $this->_opern_Dao->getOpernByCid ( $categoryid );
        return $list ? $list : array ();
    }

    /*
     * 查找曲谱下歌曲
     */

    public function getMusicList ($uid , $sid , $oid = 0)
    {
        Service_UserModel::userVail ( $uid , $sid );
        if ( $oid == 0 ) throw new YExceptionTool ( YExceptionTool::PARAMPACK_ERROR );
        $userinfo    = $this->_userDao->getUserInfo ( array ( 'id' => $uid ) );
        $operninfo   = $this->_opern_Dao->getOperns ( array ( $oid ) );
        $operninfo   = $operninfo[ 0 ];
        $musiclist   = $this->_music_Dao->getMusicsByOid ( $oid );
        $musicids    = Tools_Utilsareas::arrayDump ( $musiclist , 'id' );
        $homeSvc     = new Service_HomeModel();
        $homeDataSvc = new Dao_StudenthomeworkdateModel();
        //如果是学生
        if ( $userinfo[ 'type' ] == self::STUDENT_TYPE )
        {
            foreach ( $musiclist as $key => $music )
            {
                $maxStar                    = $homeSvc->getmusicStar ( $uid , $music[ 'id' ] );
                $musiclist[ $key ][ 'starnum' ] = $maxStar;
            }
        }
        $operninfo[ 'musiclist' ] = $musiclist;
        return $operninfo ? $operninfo : array ();
    }

    /*
     * 添加最近使用
     */

    public function addusedopern ($uid , $sid , $oid = 0)
    {
        Service_UserModel::userVail ( $uid , $sid );
        if ( $oid == 0 ) throw new YExceptionTool ( YExceptionTool::PARAMPACK_ERROR );
        $res = $this->_usedopern_Dao->addused ( $uid , $oid );
        return $res;
    }

    /*
     * 查看最近使用
     */

    public function getusedopern ($uid , $sid)
    {
        Service_UserModel::userVail ( $uid , $sid );
        $userinfo                = $this->_userDao->getUserInfo ( array ( 'id' => $uid ) );
        $usedlist = $this->_usedopern_Dao->getlist ( $uid , 10 );
        $oids     = array_unique ( array_column ( $usedlist , 'oid' ) );
        $data     = array ();
        if ( isset ( $oids ) && ! empty ( $oids ) )
        {
            $operns         = $this->_opern_Dao->getOperns ( $oids );
            $opernsbriefing = Tools_Utilsareas::indexPull ( $operns , 'id' );

            foreach ( $oids as $k => $v )
            {
                $data[] = $opernsbriefing[ $v ];
            }
        }elseif($userinfo['type'] == self::TEACHER_TYPE && empty ( $oids )){   //如果是老师,没有最近使用,就随机给几个琴谱
           
            $operns = $this->_opern_Dao->getOpernByCid(0,9);
            $data = $operns;
        }
        return $data;
    }
    
    
    public function getgrasp($uid,$sid)
    {
        Service_UserModel::userVail ( $uid , $sid );
        $grasplist = $this->_graspDao->getGraspList ( $uid);
        $mids     = array_unique ( array_column ( $grasplist , 'mid' ) );
        $data     = array ();
        if ( isset ( $mids ) && ! empty ( $mids ) )
        {
            $musics         = $this->_music_Dao->getMusic ( $mids );
            $opernsbriefing = Tools_Utilsareas::indexPull ( $musics , 'id' );
            foreach ( $mids as $k => $v )
            {
                $data[] = $opernsbriefing[ $v ];
            }
        }
        return $data;
    }

}
